﻿#include "FITKAbstractVAOneXMLHelper.h"
#include "rapidxml.hpp"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITKAcousticsModel/FITKAbstractSEAFace.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysAbsShell.h"
#include <QDebug>

int IO::FITKAbstractVAOneXMLHelper::getMaterialObjectIdByName(QString name)
{
    //获取全局数据管理器
    auto globalData = FITKAPP->getGlobalData();
    if (!globalData) return -1;// 如果转换失败，材料名称无法填写

    //获取材料管理器
    auto materialsDataManager = getPhysics()->getMaterialManager();
    if (!materialsDataManager) return -1;

    //根据名称获取材料类指针
    Interface::FITKAbstractMaterial* mat = materialsDataManager->getDataByName(name);
    if (!mat) return -1;

    return mat->getDataObjectID();
    // 没有找到匹配项，返回 -1
}

QString IO::FITKAbstractVAOneXMLHelper::getMaterialObjectNameById(int id)
{
    //获取全局数据管理器
    auto globalData = FITKAPP->getGlobalData();
    if (!globalData) return "";// 如果转换失败，材料名称无法填写

    //获取材料管理器
    auto materialsDataManager = getPhysics()->getMaterialManager();
    if (!materialsDataManager) return "";
 
    //根据名称获取材料类指针
    Interface::FITKAbstractMaterial* mat = materialsDataManager->getDataByID(id);
    if (!mat) return "";

    return mat->getDataObjectName();
    // 没有找到匹配项，返回 -1
}

int IO::FITKAbstractVAOneXMLHelper::getSpectraObjectIdByName(QString name)
{
    //获取全局数据管理器
    auto globalData = FITKAPP->getGlobalData();
    if (!globalData) return -1;// 如果转换失败，材料名称无法填写

    auto spectraManager = getPhysics()->getFITKAcousticsSpectraManager();
    if (!spectraManager) return -1;

    auto data = spectraManager->getDataByName(name);
    if (data) return data->getDataObjectID();

    return -1;
}

QString IO::FITKAbstractVAOneXMLHelper::getSpectraObjectNameByID(int id)
{
    //获取全局数据管理器
    auto globalData = FITKAPP->getGlobalData();
    if (!globalData) return "";// 如果转换失败，材料名称无法填写

    auto data = getSpectraManager()->getDataByID(id);
    if (data) return data->getDataObjectName();

    return "";
}

int IO::FITKAbstractVAOneXMLHelper::getAcousticsObjectIdByName(QString name)
{
    //获取全局数据管理器
    auto globalData = FITKAPP->getGlobalData();
    if (!globalData) return -1;// 如果转换失败，材料名称无法填写

    auto NCTManager = getPhysics()->getFITKAcousticsNCTManager();
    if (!NCTManager) return -1;

    auto data = NCTManager->getDataByName(name);
    if (data) return data->getDataObjectID();

    return -1;
}

int IO::FITKAbstractVAOneXMLHelper::getPhyPropertyId(QString name)
{
    int id = -1;

    //获取物理属性管理器
    Acoustics::FITKAcousticsPhysicsPropManager* dataManager = getPhysics()->getPhysicsPropManager();
    if (nullptr == dataManager) return id;

    Acoustics::FITKAcousticsAbsPhysicsProp* data = dataManager->getDataByName(name);
    if (nullptr != data) return data->getDataObjectID();

    qDebug()<<"get phy property id failed: name ="<<name;

    return -1;
}

QString IO::FITKAbstractVAOneXMLHelper::getPhyPropertyName(int id)
{
    //获取物理属性管理器
    Acoustics::FITKAcousticsPhysicsPropManager* dataManager = getPhysics()->getPhysicsPropManager();
    if (nullptr == dataManager) return "";

    Acoustics::FITKAcousticsAbsPhysicsProp* data = dataManager->getDataByID(id);
    if (nullptr != data) return data->getDataObjectName();

    qDebug()<<"get phy property name failed: id ="<<id;

    return "";
}

int IO::FITKAbstractVAOneXMLHelper::getFaceObjId(int faceId)
{
    int id = -1;

    Acoustics::FITKSEAFaceManager *dataManager = getSEAModel()->getSEAFaceManager();
    if (nullptr == dataManager) return id;

    int count = dataManager->getDataCount();
    for (int i = 0; i < count; ++i)
    {
        Acoustics::FITKAbstractSEAFace* data = dynamic_cast<Acoustics::FITKAbstractSEAFace*>(dataManager->getDataByIndex(i));
        if (nullptr == data) continue;

        int id = data->getUserData(Core::FITKUserRole).toInt();
        if (id == faceId) return data->getDataObjectID();
    }
    qDebug()<<"get face obj id failed: id ="<<faceId;

    return id;
}

int IO::FITKAbstractVAOneXMLHelper::getNCTID(QString name)
{
    //获取声学包管理器
    Acoustics::FITKAcousticsNCTManager* nctMgr = getPhysics()->getFITKAcousticsNCTManager();
    if (nullptr == nctMgr) return -1;

    Acoustics::FITKAcousticsAbsNCT* nctData = nctMgr->getDataByName(name);
    if (nullptr != nctData) return nctData->getDataObjectID();
 
    qDebug()<<"get NCT id failed: name ="<<name;

    return -1;
}

QString IO::FITKAbstractVAOneXMLHelper::getNCTName(int id)
{
    if (-1 == id) return "";

    //获取声学包管理器
    Acoustics::FITKAcousticsNCTManager* nctMgr = getPhysics()->getFITKAcousticsNCTManager();
    if (nullptr == nctMgr) return "";

    Acoustics::FITKAcousticsAbsNCT* nctData = nctMgr->getDataByID(id);
    if (nullptr != nctData) return nctData->getDataObjectName();
    
    qDebug()<<"get NCT name failed: id ="<<id;

    return "";
}

int IO::FITKAbstractVAOneXMLHelper::getModalDensityId(QString name)
{
    QList<Acoustics::FITKAcousticsAbstractSpectra*> spectraList = getSpectraManager()->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::ModalDensity);
    for (auto key : spectraList)
    {
        if (name == key->getDataObjectName()) //数据有效时使能开关和保存数据
            return key->getDataObjectID();
    }

    return -1;
}

QString IO::FITKAbstractVAOneXMLHelper::getModalDensityName(int id)
{
    QList<Acoustics::FITKAcousticsAbstractSpectra*> spectraList = getSpectraManager()->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::ModalDensity);
    for (auto key : spectraList)
    {
        if (id == key->getDataObjectID())
            return key->getDataObjectName();
    }

    qDebug()<<"get modal density name failed: id ="<<id;

    return "";
}

int IO::FITKAbstractVAOneXMLHelper::getDampingId(QString name)
{
    //将字符串信息转换成ojectid
    QList<Acoustics::FITKAcousticsAbstractSpectra*> spectraList = getSpectraManager()->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Damping);
    for (auto key : spectraList)
    {
        if (name == key->getDataObjectName())
            return key->getDataObjectID();
    }

    return -1;
}

QString IO::FITKAbstractVAOneXMLHelper::getDampingName(int id)
{
    if (-1 == id) return "";

    //将字符串信息转换成ojectid
    QList<Acoustics::FITKAcousticsAbstractSpectra*> spectraList = getSpectraManager()->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Damping);
    for (auto key : spectraList)
    {
        if (id == key->getDataObjectID())
            return key->getDataObjectName();
    }

    return "";
}

bool IO::FITKAbstractVAOneXMLHelper::strToBool(QString str)
{
    //支持两种形式的处理
    if (("true" == str) || ("1" == str)) return true;

    return false;
}

QString IO::FITKAbstractVAOneXMLHelper::boolToStr(bool flag)
{
    if (true == flag) return QString("true");

    return QString("false");
}

Acoustics::FITKAbstractSEASubSys * IO::FITKAbstractVAOneXMLHelper::getShellObjByXMLFace_id(int faceID)
{
    //获取子系统数据
    int count = getSEASubSysManager()->getDataCount();
    for (int i = 0; i < count; ++i)
    {
        Acoustics::FITKAbstractSEASubSysShell* shellData = dynamic_cast<Acoustics::FITKAbstractSEASubSysShell*>(getSEASubSysManager()->getDataByIndex(i));
        if (!shellData) continue;

        Acoustics::FITKAbstractSEAFace* faceData = shellData->getFaceObj();
        int face_id = faceData->getUserData(Core::FITKUserRole).toInt();
        if (faceID = face_id) return shellData;
    }
    return nullptr;
}

int IO::FITKAbstractVAOneXMLHelper::getFaceIdByShellSysId(int shellSysId)
{
    Acoustics::FITKAbstractSEASubSysShell* shellData = dynamic_cast<Acoustics::FITKAbstractSEASubSysShell*>(getSEASubSysManager()->getDataByID(shellSysId));
    if (shellData == nullptr) return 0;

    return shellData->getFaceID();
}

Acoustics::FITKAcousticsFESubSysManager * IO::FITKAbstractVAOneXMLHelper::getFESubSystemManager()
{
    return getFEModel()->getFESubSysManager();
}

Acoustics::FITKAcousticsSpectraManager * IO::FITKAbstractVAOneXMLHelper::getSpectraManager()
{
    return getPhysics()->getFITKAcousticsSpectraManager();
}

Acoustics::FITKAcousticsMaterialManager * IO::FITKAbstractVAOneXMLHelper::getMaterialManager()
{
    return getPhysics()->getMaterialManager();
}

Acoustics::FITKAcousticsPhysicsPropManager * IO::FITKAbstractVAOneXMLHelper::getPhysicalPropertiesManager()
{
    return getPhysics()->getPhysicsPropManager();
}

Acoustics::FITKAcousticsFESubSysElementList * IO::FITKAbstractVAOneXMLHelper::getFESubSysElementList()
{
    return getFEModel()->getFESubSysElementList();
}

Acoustics::FITKAcousticsFEModel * IO::FITKAbstractVAOneXMLHelper::getFEModel()
{
    return getDiscModel()->getFEModel();
}

Acoustics::FITKAcousticsSEAModel * IO::FITKAbstractVAOneXMLHelper::getSEAModel()
{
    return getDiscModel()->getSEAModel();
}

Acoustics::FITKAcousticsPhysics * IO::FITKAbstractVAOneXMLHelper::getPhysics()
{
    return FITKAPP->getGlobalData()->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
}

Acoustics::FITKAcousticsDiscModel * IO::FITKAbstractVAOneXMLHelper::getDiscModel()
{
    return FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
}

Acoustics::FITKAcousticsLoadManager * IO::FITKAbstractVAOneXMLHelper::getLoadManager()
{
    return getPhysics()->getFITKAcousticsLoadManager();
}

Acoustics::FITKJunctionManager * IO::FITKAbstractVAOneXMLHelper::getJunctionManager()
{
    return getDiscModel()->getJunctionManager();
}

Acoustics::FITKAcousticsSEASubSysManager * IO::FITKAbstractVAOneXMLHelper::getSEASubSysManager()
{
    return getSEAModel()->getSEASubSysManager();
}

rapidxml::xml_node<char>* IO::FITKAbstractVAOneXMLHelper::createChildNode(rapidxml::xml_node<char>* parentNode, const char * childNodeName, rapidxml::node_type type)
{
    if (parentNode == nullptr || childNodeName == nullptr || type == nullptr) return nullptr;

    rapidxml::xml_node<char>* childNode = parentNode->document()->allocate_node(type, childNodeName);
    if (childNode == nullptr) return nullptr;
    parentNode->append_node(childNode);

    return childNode;
}

rapidxml::xml_node<char>* IO::FITKAbstractVAOneXMLHelper::createThirdLevelNode(rapidxml::xml_node<char>* primaryNode, const char* secondLevelName, const char* thirdLevelName, rapidxml::node_type type)
{
    if (primaryNode == nullptr || secondLevelName == nullptr || thirdLevelName == nullptr || type == nullptr) return nullptr;

    // 创建二级节点
    rapidxml::xml_node<char>* secondLevelNode = createChildNode(primaryNode, secondLevelName, type);
    if (secondLevelNode == nullptr) return nullptr;

    // 创建三级节点
    rapidxml::xml_node<char>* thirdLevelNode = createChildNode(secondLevelNode, thirdLevelName, type);
    if (thirdLevelNode == nullptr) return nullptr;

    return thirdLevelNode;
}
